# encoding: utf-8
'''
Created on 2018年5月31日

@author: mengqiang.song
'''
from PIL import Image
import os
import numpy as np
os.environ['TF_CPP_MIN_LOG_LEVEL']='2' #屏蔽掉tensorFlow的一些警告

class ImgUtil(object):
    def __init__(self):
        print('123')
    #裁剪压缩图片  
    @staticmethod
    def imgResize(org_img,dst_img,dst_w,dst_h,save_q=75):  
              
        im = Image.open(org_img)  
        ori_w,ori_h = im.size  
      
        dst_scale = float(dst_h) / dst_w #目标高宽比  
        ori_scale = float(ori_h) / ori_w #原高宽比  
      
        if ori_scale >= dst_scale:  
            #过高  
            width = ori_w  
            height = int(width*dst_scale)  
      
            x = 0  
            y = (ori_h - height) / 3  
              
        else:  
            #过宽  
            height = ori_h  
            width = int(height*dst_scale)  
      
            x = (ori_w - width) / 2  
            y = 0  
      
        #裁剪  
        box = (x,y,width+x,height+y)  
        #这里的参数可以这么认为：从某图的(x,y)坐标开始截，截到(width+x,height+y)坐标  
        #所包围的图像，crop方法与php中的imagecopy方法大为不一样  
        newIm = im.crop(box)  
        im = None  
      
        #压缩  
        ratio = float(dst_w) / width  
        newWidth = int(width * ratio)  
        newHeight = int(height * ratio)  
        newIm.resize((newWidth,newHeight),Image.ANTIALIAS).save(dst_img,quality=save_q) 
        
        return newIm;
    @staticmethod
    def imgCut0(oriImgAbPath,dstImgAbPath,dstW,dstH):
        """
                    切出一张图片
        """
        img = Image.open(oriImgAbPath)
        oriW,oriH = img.size
        if oriW<dstW or oriH<dstH:
            return -1
        x = (oriW-dstW)/2
        y = (oriH-dstH)/2
        x2 = x+dstW
        y2 = y+dstH
        box = (x,y,x2,y2)
        newIm = img.crop(box)
        imgl = newIm.convert('L')
        imgl.save(dstImgAbPath,quality=75) 
        return 1
    #图片转方图
    @staticmethod
    def imgCut(rgbimg,basePath,preffix,suffix,dstW,dstH,icount):
        """
                    原图中切除方图，从图片中心切，但是会有随机的故意切偏，共icount张
        """
        img = Image.open(rgbimg)
        oriW,oriH = img.size
        if oriW<dstW or oriH<dstH:
            return []
        cs = []
        for i in range(0,icount):
            c = []
            x = 0
            y = 0
            while True:
                if oriW-dstW == 0:
                    c.append(0)
                    break
                x = np.random.randint(0,oriW-dstW)
                if x>(oriW/2-5*dstW/6) and x<(oriW/2-dstW/6) :
                    c.append(x)      
                    break;
            while True:
                if oriH-dstH == 0:
                    c.append(0)
                    break
                y = np.random.randint(0,oriH-dstH)
                if y>(oriH/2-5*dstH/6) and y<(oriH/2-dstH/6) :
                    c.append(y)      
                    break;
            c.append(x+dstW)
            c.append(y+dstH)
            cs.append(c)
        index = 0
        for bparam in cs:
            box = (bparam[0],bparam[1],bparam[2],bparam[3])
            newIm = img.crop(box)
            imgl = newIm.convert('L')
            imgl.save(basePath+"\\"+preffix+str(index)+suffix,quality=75) 
            index = index+1
            
            
    #灰度图转二维数组
    @staticmethod
    def himg2Matrix(himg):
        return 0
    
class ArrayUtil(object):
    #二维数组切割成方阵
    @staticmethod
    def Matrix2A(m):
        return 0